package org.example.webdav.resources;

import com.github.sardine.DavResource;
import com.github.sardine.Sardine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.UrlResource;
import org.springframework.core.io.WritableResource;

import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.channels.WritableByteChannel;

public class WebDavResource extends UrlResource implements WritableResource {

    private static final Logger logger = LoggerFactory.getLogger(WebDavResource.class);

    protected Sardine client;
    protected String path;

    public WebDavResource(final Sardine client, final String path) throws IOException, URISyntaxException {
        super(new URI("http://"+path));
        this.client = client;
        this.path = getURL().toString();
    }

    @Override
    public boolean exists() {
        try {
            return client.exists(path);
        } catch (IOException e) {
            logger.error("Error checking file at {}", path, e);
            return false;
        }
    }

    @Override
    public boolean isReadable() {
        return true;
    }

    @Override
    public boolean isOpen() {
        return false;
    }

    @Override
    public File getFile() throws IOException {
        throw new IOException(getDescription() + " can't be resolved to a File");
    }

    @Override
    public long contentLength() throws IOException {
        return getResource().getContentLength();
    }

    @Override
    public long lastModified() throws IOException {
        return getResource().getModified().getTime();
    }

    @Override
    public String getFilename() {
        try {
            return getResource().getName();
        } catch (IOException e) {
            logger.error("Error getting file name for {}", path, e);
            return path;
        }
    }

    @Override
    public String getDescription() {
        try {
            return getResource().getDisplayName();
        } catch (IOException e) {
            logger.error("Error getting file description for {}", path, e);
            return null;
        }
    }

    @Override
    public InputStream getInputStream() throws IOException {
        return client.get(path);
    }

    protected DavResource getResource() throws IOException {
        return client.list(path, 1, true).stream().findFirst().orElseThrow(FileNotFoundException::new);
    }

    @Override
    public boolean isWritable() {
        return WritableResource.super.isWritable();
    }

    @Override
    public OutputStream getOutputStream() throws IOException {
        PipedInputStream in = new PipedInputStream();
        PipedOutputStream out = new PipedOutputStream(in);
        new Thread(() -> {
            try {
                    client.put(path, in,"text/csv", true);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();
        return out;
    }

    @Override
    public WritableByteChannel writableChannel() throws IOException {
        return WritableResource.super.writableChannel();
    }
}
